package com.nali.dal.statement.query;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.nali.dal.expression.PropertyExpression;
import com.nali.dal.expression.query.Criteria;

public class GroupColumn implements ColumnUnitConfig {
	private static class GroupColumnUnit implements ColumnUnit {
		private String column;
		private String name;
		public GroupColumnUnit(String column, String name) {
			super();
			this.column = column;
			this.name = name;
		}

		@Override
		public String getName() {
			return name;
		}

		@Override
		public Object getColumnValue(Map<String, Object> lineDatas) {
			return lineDatas.get(column);
		}
	}

	private String column;
	private PropertyExpression<Criteria> propertyExpression;
	private GroupColumnUnit columnUnit;

	public GroupColumn(String name, Criteria groupFunction, String orgColumn) {
		propertyExpression = new PropertyExpression<Criteria>(orgColumn, groupFunction);
		column=getGroupColumn(groupFunction, orgColumn);
		columnUnit = new GroupColumnUnit(column, name);
	}

	private static String getGroupColumn(Criteria groupFunction, String orgColumn) {
		return orgColumn+"_"+groupFunction.name();
	}

	@Override
	public ColumnUnit getColumnUnit() {
		return this.columnUnit;
	}

	@Override
	public List<String> getColumns() {
		return Arrays.asList(column);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PropertyExpression<Criteria>> getColumnCriterias() {
		return Arrays.asList(propertyExpression);
	}

}
